<?php

namespace App\Http\Controllers\background;

use App\Http\Controllers\Controller;
use App\Models\Admin\InterfaceConfiguration;
use App\Models\Mysql\XnGoodsCarmyType;
use App\Models\Mysql\XnOrder;
use App\Models\Mysql\XnTerraceProfit;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ProfitController extends Controller
{
    public function profitlist(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $type = $request->post('order_type','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        $condition[] = ['order_type','=',$type];
        $condition[] = ['recharge_status','=',1];

        $list = XnTerraceProfit::orderBy('ctime','desc')->paginate(10) -> toArray();
       if($list['data']){
            foreach($list['data'] as $k => $v){
                $list['data'][$k]['allmoney'] = $this -> getsummoneys($condition,$v['supplier_status'],1);
                $list['data'][$k]['allcount'] = $this -> getsummoneys($condition,$v['supplier_status'],2);
            }
        }
        return successly($list);
    }

    public function getsummoneys($condition,$supplier_status,$type){
        $arr = [];
        if($supplier_status == 10000){
            $arr = [1,2];
        }else{
            $condition[] = ['goods_belong','=',$supplier_status];
        }
        $money = XnOrder::where($condition)
            -> whereNotIn('goods_belong',$arr)
            -> select(DB::raw('SUM(all_money-unitPrice*goods_num) as allprofit,COUNT(*) as allcount'))
            -> first()->toArray();

        if($type == 1){
            return round($money['allprofit'],4);
        }else if($type == 2){
            return $money['allcount'];
        }
    }

    public function profitlistdetail(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $supplier_status = $request->post('Supplier_status','');
        $type = $request->post('order_type','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        $condition[] = ['order_type','=',$type];
        $condition[] = ['recharge_status','=',1];
        $arr = [];
        if($supplier_status == 10000){
            $arr = [1,2];
        }else{
            $condition[] = ['goods_belong','=',$supplier_status];
        }
        $list = XnOrder::where($condition)
            -> whereNotIn('goods_belong',$arr)
            -> select(DB::raw('recharge_time,goods_belong,mechant_name,goods_name,order_sn,unitPrice,price,goods_num,all_money,ROUND(all_money-unitPrice*goods_num,4) as allprofit,children_supplier_status'))
            -> orderBy('recharge_time','desc')
            -> paginate(10)
            -> toArray();
        return successly($list);
    }

    public function profitdetailed(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $type = $request->post('order_type','');
        $supplier_status = $request->post('Supplier_status','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        $condition[] = ['order_type','=',$type];
        $condition[] = ['recharge_status','=',1];
        $arr = [];
        if($supplier_status == 10000){
            $arr = [1,2];
        }else{
            $condition[] = ['goods_belong','=',$supplier_status];
        }
        $list = XnOrder::where($condition)
            -> whereNotIn('goods_belong',$arr)
            -> select(DB::raw('recharge_time,goods_belong,mechant_name,goods_name,order_sn,unitPrice,price,goods_num,all_money,ROUND(all_money-unitPrice*goods_num,4) as allprofit,children_supplier_status'))
            -> orderBy('recharge_time','desc')
            -> paginate(10)
            -> toArray();
        //print_r($list);
        foreach($list['data'] as $k =>$v){
            $title = InterfaceConfiguration::find($v['goods_belong']);
            if(!empty($title)){
                $list['data'][$k]['goods_belong'] = InterfaceConfiguration::find($v['goods_belong']) -> title;
            }else{
                $list['data'][$k]['goods_belong'] = XnGoodsCarmyType::where('type',$v['goods_belong']) -> value('name');
            }

        }
        return successly($list);
    }

    public function getsummoney(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $type = $request->post('order_type','');
        $supplier_status = $request->post('Supplier_status','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        $condition[] = ['order_type','=',$type];
        $condition[] = ['recharge_status','=',1];
        $arr = [];
        if($supplier_status == 10000){
            $arr = [1,2];
        }else{
            $condition[] = ['goods_belong','=',$supplier_status];
        }
        $money = XnOrder::where($condition)
            -> whereNotIn('goods_belong',$arr)
            -> select(DB::raw('SUM(all_money-unitPrice*goods_num) as allprofit'))
            -> first()
            -> toArray();
        return successly($money);
    }
}
